package au.edu.swin.videoshop.bean;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import au.edu.swin.videoshop.service.MiscUtil;

public class TitleMedium implements Serializable{
	private Long mTitleMediumId;
	private Title mTitle;
	private MediumType mMediumType;
	private BigDecimal mRentalRate;
	private int mMaxRentalPeriod;
	private BigDecimal mPenaltyRate;
	private List mCopies;
	private List mAvailableStores;
	
	/**
	 * @return the titleMediumId
	 */
	public Long getTitleMediumId() {
		return mTitleMediumId;
	}

	/**
	 * @param titleMediumId the titleMediumId to set
	 */
	public void setTitleMediumId(Long titleMediumId) {
		mTitleMediumId = titleMediumId;
	}

	/**
	 * @return the title
	 */
	public Title getTitle() {
		return mTitle;
	}
	
	/**
	 * @param title the title to set
	 */
	public void setTitle(Title title) {
		mTitle = title;
	}
	
	/**
	 * @return the mediumType
	 */
	public MediumType getMediumType() {
		return mMediumType;
	}
	
	/**
	 * @param mediumType the mediumType to set
	 */
	public void setMediumType(MediumType mediumType) {
		mMediumType = mediumType;
	}
	
	/**
	 * @return the rentalRate
	 */
	public BigDecimal getRentalRate() {
		return mRentalRate;
	}
	
	/**
	 * @param rentalRate the rentalRate to set
	 */
	public void setRentalRate(BigDecimal rentalRate) {
		mRentalRate = MiscUtil.round(rentalRate);
	}
	
	/**
	 * @return the maxRentalPeriod
	 */
	public int getMaxRentalPeriod() {
		return mMaxRentalPeriod;
	}
	
	/**
	 * @param maxRentalPeriod the maxRentalPeriod to set
	 */
	public void setMaxRentalPeriod(int maxRentalPeriod) {
		mMaxRentalPeriod = maxRentalPeriod;
	}
	
	/**
	 * @return the penaltyRate
	 */
	public BigDecimal getPenaltyRate() {
		return mPenaltyRate;
	}
	
	/**
	 * @param penaltyRate the penaltyRate to set
	 */
	public void setPenaltyRate(BigDecimal penaltyRate) {
		mPenaltyRate = MiscUtil.round(penaltyRate);
	}
	
	public boolean isAvailable() {
		return (mMaxRentalPeriod > 0 && 
				mPenaltyRate != null &&
				mRentalRate != null);
	}
	
	/**
	 * @return the copies
	 */
	public List getCopies() {
		return mCopies;
	}
	
	/**
	 * @param copies the copies to set
	 */
	public void setCopies(List copies) {
		mCopies = copies;
		
		if (mCopies != null) {
			mAvailableStores = new ArrayList();
			
			for (Iterator i = mCopies.iterator(); i.hasNext();) {
				TitleCopy copy = (TitleCopy)i.next();
				
				if (!mAvailableStores.contains(copy.getStore())) {
					mAvailableStores.add(copy.getStore());
				}
			}
		}
	}
	
	public void addCopy(String id, Store store) {
		if (mCopies == null) {
			mCopies = new ArrayList();
		}
		
		TitleCopy copy = new TitleCopy();
		copy.setTitleCopyId(id);
		copy.setMedium(this);
		copy.setStore(store);
		
		mCopies.add(copy);
		
		if (mAvailableStores == null) {
			mAvailableStores = new ArrayList();
		}
		if (!mAvailableStores.contains(store)) {
			mAvailableStores.add(store);
		}
	}
	
	public void removeCopy(TitleCopy copy) {
		if (mCopies != null) {
			mCopies.remove(copy);
		}
	}
	
	public List getAvailableStores() {
		return mAvailableStores;
	}
}
